package com.qdlc.p2p.dal.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.qdlc.p2p.common.exception.AccountException;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dao.AccountDao;
import com.qdlc.p2p.dal.dto.Account;
import com.qdlc.p2p.dal.model.AccountModel;
import com.qdlc.p2p.dal.mybatis.model.mapper.AccountMapper;
import com.qdlc.p2p.dal.mybatis.pager.PageProxyBuilder;
import com.qdlc.p2p.dal.mybatis.pager.core.PageParameter;

@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {

	@Resource
	private AccountMapper accountMapper;

	@Override
	public void save(Account account) {
		accountMapper.insert(account);
	}

	@Override
    public int update(Account account) {
        // 账户资金CHECK
        checkAccount(account);

        // 更新
		return accountMapper.update(account);
    }

	@Override
	public void updateTotalAndUseMoney(Account account) {
		// 账户资金CHECK
		checkAccount(account);

		accountMapper.updateTotalAndUseMoney(account);
	}

	@Override
	public void updateTotalAndNoUseMoney(Account account) {
		// 账户资金CHECK
		checkAccount(account);

		accountMapper.updateTotalAndNoUseMoney(account);
	}

	@Override
	public void updateUseMoneyAndNoUseMoney(Account account) {
		// 账户资金CHECK
		checkAccount(account);

		accountMapper.updateUseMoneyAndNoUseMoney(account);
	}

	@Override
	public void updateTotalAndCollection(Account account) {
		// 账户资金CHECK
		checkAccount(account);

		accountMapper.updateTotalAndCollection(account);
	}

	@Override
	public void updateUseMoneyAndCollection(Account account) {
		// 账户资金CHECK
		checkAccount(account);

		accountMapper.updateUseMoneyAndCollection(account);
	}

	@Override
	public void updateNoUseMoneyAndCollection(Account account) {
		// 账户资金CHECK
		checkAccount(account);

		accountMapper.updateNoUseMoneyAndCollection(account);
	}

	@Override
	public int updateSinaProfit(long accountId, double yesterdayBonus, double totalBonus) {
		return accountMapper.updateSinaProfit(accountId, yesterdayBonus, totalBonus);
	}

	@Override
	public Account findById(long id) {
		return accountMapper.findById(id);
	}

	@Override
	public Account findByUserId(long userId) {
		return accountMapper.findByUserId(userId);
	}

	@Override
	public PageDataList<AccountModel> list(AccountModel model) {
		PageProxyBuilder<AccountMapper> builder = PageProxyBuilder.newBuilder(AccountMapper.class);
        builder.setPageParameter(model.getPage(),model.getRows());
		List<AccountModel> accountList = builder.createProxy().list(model);
		PageParameter page = builder.getPageParameter();
        return new PageDataList<AccountModel>(page.convert2Page(),accountList);
	}

	@Override
	public double findUseMoneyByUserId(long userId) {
		return accountMapper.findUseMoneyByUserId(userId);
	}

	@Override
	public double sumAllUseMoney() {
		return accountMapper.sumAllUseMoney();
	}

	@Override
	public double sumUseMoney() {
		return accountMapper.sumUseMoney();
	}

	/*
     * 账户资金不能为负数
     */
    private void checkAccount(Account account) {
        if (null != account) {
            if (account.getTotal() < 0) {
                throw new AccountException("账户总额不能小于0！总额：" + account.getTotal(), AccountException.TYPE_JSON);
            }
            if (account.getUseMoney() < 0) {
                throw new AccountException("账户可用余额不能小于0！可用余额：" + account.getUseMoney(), AccountException.TYPE_JSON);
            }
            if (account.getNoUseMoney() < 0) {
                throw new AccountException("账户冻结金额不能小于0！冻结金额：" + account.getNoUseMoney(), AccountException.TYPE_JSON);
            }
			if(account.getCollection() < 0 ) {
				throw new AccountException("账户待收金额不能小于0！代收金额：" + account.getNoUseMoney(), AccountException.TYPE_JSON);
			}
        }
    }
}
